/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.soc.cs5226.gui.entities;

import edu.soc.cs5226.gui.entities.util.Constants;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author huijia
 */
public class RedoLogBufferProcessList {
    public dayObject processTreeNode(Timestamp now, List<xObject> xList, HealthStatus hs)
    {
        long total24Number = 0;
        long total24RedoLogSpaceRequest = 0;
        
        Iterator<xObject> xIter = xList.iterator();
        while (xIter.hasNext())
        {
            long totalNumber = 0;
            long totalRedoLogSpaceRequest = 0;
            
            xObject currX = xIter.next();
            Iterator<yObject> yIter = currX.getyObjects().iterator();
            while (yIter.hasNext())
            {
                long subtotalNumber = 0;
                long subtotalRedoLogSpaceRequest = 0;
                yObject currY = yIter.next();
                Iterator<DBParameter> rlbIter = currY.getObj().iterator();
                while (rlbIter.hasNext())
                {
                    RedoLogBuffer rlbObj = (RedoLogBuffer)rlbIter.next();
                    total24RedoLogSpaceRequest = total24RedoLogSpaceRequest + rlbObj.getRedoLogSpaceRequest();
                    total24Number++;
                    
                    totalRedoLogSpaceRequest = totalRedoLogSpaceRequest + rlbObj.getRedoLogSpaceRequest();
                    totalNumber++;
                    
                    subtotalRedoLogSpaceRequest = subtotalRedoLogSpaceRequest + rlbObj.getRedoLogSpaceRequest();
                    subtotalNumber++;
                }
                RedoLogBuffer treenodeY = (RedoLogBuffer)currY.getyObj();
                treenodeY.setRedoLogSpaceRequest(calcAverage(subtotalRedoLogSpaceRequest,subtotalNumber));
                treenodeY.setLb_healthStatus(processRedoLogBuf(treenodeY, hs));
            }
            RedoLogBuffer treenodeX = (RedoLogBuffer)currX.getxObj();
            treenodeX.setRedoLogSpaceRequest(calcAverage(totalRedoLogSpaceRequest,totalNumber));
            treenodeX.setLb_healthStatus(processRedoLogBuf(treenodeX,hs));
        }
        
        RedoLogBuffer treenodeRoot = (RedoLogBuffer)DBParamFactory.getDBParameter(Constants.REDOLOGBUFFER);
        treenodeRoot.setRedoLogSpaceRequest(calcAverage(total24RedoLogSpaceRequest,total24Number));
        treenodeRoot.setLb_healthStatus(processRedoLogBuf(treenodeRoot,hs));
        treenodeRoot.setdateTime(new Timestamp(now.getTime()-(24*60*60*1000)));
        
        dayObject dObject = new dayObject();
        dObject.setRootObj(treenodeRoot);
        dObject.setxObjects(xList);
        
        return dObject;
    }
    
    //--------------------------------------------------------------------------
    
    private long calcAverage(long value, long number)
    {
        if (value>0)
            return (value/number);
        else
            return 0;
    }
    
    //--------------------------------------------------------------------------
    // processLogBuffer obtains log buffer info from the database
    // and calculcates the status indicator
    // if the redoLogSpaceRequest < yellow indicator
    //    status is green.
    // if the redoLogSpaceRequest >= yellow & redoLogSpaceRequest < red indicator
    //    status is yellow
    // if the redoLogSpaceRequest >= red indicator
    //    status is red
    //--------------------------------------------------------------------------
    private String processRedoLogBuf(RedoLogBuffer rlb, HealthStatus hs)
    {
        String status = "G";
        if ((rlb.getRedoLogSpaceRequest()>=hs.getyellowThres()) && 
                (rlb.getRedoLogSpaceRequest()<hs.getredThres()))
        {
            status = "Y";
        }
        else if (rlb.getRedoLogSpaceRequest()>=hs.getredThres())
        {
            status = "R";
        }
        return status;
    } // processRedoLogBuf
}
